package Offer;

import java.util.Deque;
import java.util.LinkedList;

/*
  栈的压入,弹出序列
      循环判断栈顶元素与当前poped序列元素是否相等,相等则出栈
      * */
public class Offer31validateStackSequences {
    public boolean validateStackSequences(int[] pushed, int[] popped) {
        if(pushed==null || pushed.length==0){
            return true;
        }
        Deque<Integer> stack=new LinkedList();
        int i=0,j=0;
        while(i<pushed.length){
            stack.push(pushed[i++]);
            while(!stack.isEmpty() && stack.peek()==popped[j]){
                stack.pop();
                j++;
            }
        }
        return stack.isEmpty();
    }
}
